#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "els_vm.h"
#include "els_vm_bytecode.h"
#include "hash.h"
#include "inout.h"
#include "unit.h"
#include "els_vm_tree.h"
#include "els.h"


#ifndef MAXFUNCSTACK
#define MAXFUNCSTACK 100
#define MAXMESSAGE    MAXFUNCSTACK*80
#endif

Word els_linenumber;
Bool els_debug = 0;
char *els_parsedfile;
static FILE *fp;
static char *st;

static int fileinput (void){
    return fgetc (fp);
}

static int stringinput (void){
    return *st++;
}

char *els_openfile (char *fn){
    els_setinput (fileinput);
    if (fn == NULL){
        fp = stdin;
        fn = "(stdin)";
    }else fp = fopen (fn, "r");
    if (fp == NULL){
        static char buff[255];
        sprintf(buff, "找不到文件 `%.200s'", fn);
        return buff;
    }
    els_linenumber = 1;
    els_parsedfile = els_lexgetstring(fn)->ts.str;
    return NULL;
}
void els_closefile (void){
    if (fp != NULL && fp != stdin){
        fclose (fp);
        fp = NULL;
    }
}

char *els_openstring (char *s){
    els_linenumber = 1;
    els_setinput (stringinput);
    st = s;
    //sprintf (sn, "%s", s);
    return els_addfile (s);
}
void els_closestring (void){
    els_delfile();
}
 
